<?php

namespace iBrand\Store\Backend\Model;

use iBrand\Component\Discount\Contracts\DiscountContract;
use iBrand\Component\Discount\Contracts\DiscountItemContract;
use iBrand\Component\Goods\Models\GoodsModel;
use iBrand\Component\Goods\Models\Season;
use iBrand\Shop\Core\Models\O2oGoods;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;
use iBrand\Component\Goods\Models\Goods as BaseGoods;

class Goods extends BaseGoods implements Transformable, DiscountItemContract
{
    use TransformableTrait;

    protected $appends = ['sizeIds', 'colorIds', 'text'];

    public function hasManyAttribute()
    {
        return $this->belongsToMany('iBrand\Component\Goods\Models\Attribute', config('ibrand.app.database.prefix', 'ibrand_') . 'base_goods_attribute_relation', 'goods_id', 'attribute_id')->withPivot('attribute_value_id', 'model_id', 'attribute_value');
    }

    public function hasManyProducts()
    {
        return $this->hasMany(Product::class, 'goods_id');
    }

    public function GoodsPhotos()
    {
        return $this->hasMany('iBrand\Component\Goods\Models\GoodsPhoto', 'goods_id');
    }

    public function model()
    {
        return $this->belongsTo(GoodsModel::class, 'model_id', 'id');
    }

    public function getSizeCollection()
    {
        return $this->specValue()->where(config('ibrand.app.database.prefix', 'ibrand_') . 'base_specs_value.spec_id', 1)->get();
    }

    public function getColorCollection()
    {
        return $this->specValue()->where(config('ibrand.app.database.prefix', 'ibrand_') . 'base_specs_value.spec_id', 2)->get();
    }

    public function getSizeIdsAttribute()
    {
        $sizes = $this->getSizeCollection();
        if ($sizes && $sizes->count() > 0) {
            return $sizes->pluck('id')->implode(',');
        }

        return '';
    }

    public function getSizeListAttribute()
    {
        $sizes = $this->getSizeCollection();
        if ($sizes && $sizes->count() > 0) {
            return $sizes->pluck('name')->implode(',');
        }

        return '';
    }

    public function getColorIdsAttribute()
    {
        $colors = $this->getColorCollection();
        if ($colors && $colors->count() > 0) {
            return $colors->pluck('id')->implode(',');
        }

        return '';
    }

    public function getColorListAttribute()
    {
        $colors = $this->getColorCollection();
        if ($colors && $colors->count() > 0) {
            return $colors->pluck('name')->implode(',');
        }

        return '';
    }

    public function getTextAttribute()
    {
        return $this->goods_no . ' ' . $this->name;
    }

    public function o2oGoods()
    {
        return $this->hasMany(O2oGoods::class, 'goods_id');
    }

    public function season()
    {
        return $this->belongsTo(Season::class, 'season_id')->withDefault();
    }

    public function categories()
    {
        $prefix = config('ibrand.app.database.prefix', 'ibrand_');
        return $this->belongsToMany(Category::class, $prefix . 'base_goods_category', 'goods_id', 'category_id');
    }

    public function getKeyCode($type = 'sku')
    {
        return $this->id;
    }

    /**
     * get item relation key codes
     *
     * @return mixed
     */
    public function getChildKeyCodes()
    {
    }

    /**
     * get item type
     *
     * @return string
     */
    public function getItemType()
    {
    }

    /**
     * get item categories
     *
     * @return mixed
     */
    public function getCategories()
    {
        return $this->categories;
    }

    public function getSeasonId()
    {
        return $this->season_id;
    }
}
